// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var dialog = require("dialog");
	var validate = require("validate");
	var bulletinId;
	var router = require("../hash/pageRouter");
	var css_modify = require("./announceModify.css");
	var fileSrc = "";
	var fileSize = '';
	var fileName = "";
	var init = function(page) {
		bulletinId = page.params.bulletinId||base.getUrlParam("bulletinId");
		getAnnouceDetailInfo1(bulletinId);
		initEvent();
		currentDate();
	};
	var initEvent = function() {
		//若未登录，则返回登陆页面
		if(base.getData("sid")==null||base.getData("sid")==""||base.getData("sid")==undefined){
			top.location.href="../login/login.html";
		}
		
		//修改顶部栏的选中状态
		$(".nav_hover", window.parent.document).removeClass("head_link_1");
		$("#company", window.parent.document).addClass("head_link_1");
		// 发布公告通知
		
		$(".button_layout").on("click", "#announce_save_btn_modify", function() {
			var acceptRole = [];
			var con = $(".mr90_modify");
			var cks = con.find("input[type='checkbox']:checked");
			cks.each(function(i) {
				acceptRole.push($(cks[i]).attr('value'));
			});
			var fileArray = [];
			var fileNameArray = [];
			var fileSizeArray = [];
			var list = $(".attach_list_modify");
			list.each(function(i) {
				fileArray.push($(list[i]).attr('fileSrc'));
				fileNameArray.push($(list[i]).attr('fileName'));
				fileSizeArray.push($(list[i]).attr('fileSize'));
			});
			modifyAnnouceInfo(acceptRole, fileArray, fileNameArray, fileSizeArray);
		});
		// 删除附件
		$("#attach_part3").on("click", ".attach_delete", function() {
			$(this).parents("div.attach_list_modify").remove();
		});
		// checkbox的选中状态
		$("#objs").on("click", ".checkedLabel", function() {
			$(this).addClass("checked");
		});
		$("#objs").on("click", ".checked", function() {
			$(this).removeClass("checked");
		});
		// 添加附件的点击事件
		$("#attach_add").unbind().on('change', function() {
			uploadAnnouceFile();
		});
	};
	var uploadAnnouceFile = function() {
		var fileList = function(fileSrc, fileName, fileSize) {
				var txt = ['<div class="attach_list_modify mb10" fileSrc="' + fileSrc + '" fileName="' + fileName + '" fileSize="' + fileSize + '">',
					'<div class="attach_logo"><img src="../../../res/images/icon/logo_attach.png"></div>',
					'<div class="attach_op">',
					'<div class="attach_type">',
					'<a href="/finance/api/v1.0/downloadFile.file?fileUuid=' + fileSrc + '" download="' + fileName + '">',
					'附件：'+fileName+'</a></div>',
					'<div class="attach_delete">删除</div>',
					'<div class="hr"></div>',
				].join('');
				$("#attach_part3").append(txt);
			}
		if ($("#attach_add").val() != "") {
			$.ajaxFileUpload({
				url: "/finance/api/v1.0/uploadFile.json", //你处理上传文件的服务端
				secureuri: false,
				fileElementId: 'attach_add',
				dataType: 'json',
				success: function(data) {
					fileSrc = data.data[0].fileUuid;
					fileName = data.data[0].fileName;
					fileSize = data.data[0].fileSize;
					fileList(fileSrc, fileName, fileSize);
				},
				complete: function(xml) {
					$("#attach_add").unbind().on('change', function() {
						uploadAnnouceFile();
					});
				},
				error: function(data, status, e) {
					if (data.msg == null) {
						base.alert(2, "文件太大无法上传");
					} else {
						base.alert(2, data.msg);
					}
				}
			});
		} else {
			util.alert("请选择文件！");
		}
	};
	// 设定当前年月日
	var currentDate = function() {
		var time = base.nowDate();
		time = time[0] + "-" + time[1] + "-" + time[2];
		$("#start_input").val(time);
		$("#end_input").val(time);
	};
	
	var getAnnouceDetailInfo1 = function(bulletinId) {
		base.fnAjax({
			action: "inquireBulletinDetail.json",
			oData: {
				bulletinId: bulletinId
			},
			fnSuccess: function(data) {
				if (data.status == 200) {
					$("#annouce_title_modify").val(data.datas[0].title);
					$("#start_input_modify").val(data.datas[0].effectiveStartDate.substr(0, 10));
					$("#end_input_modify").val(data.datas[0].effectiveEndDate.substr(0, 10));
					$("#annouce_detail_modify").val(data.datas[0].content);
					$.each(data.datas[0].audiences, function() {
						switch (this) {
							case '2':
								$(".column_content").find($("#purchase_modify")).attr("checked", "checked");
								break;
							case '3':
								$(".column_content").find($("#supplier_modify")).attr("checked", "checked");
								break;
							case '1':
								$(".column_content").find($("#system_modify")).attr("checked", "checked");
								break;
							default:
								return "不详";
								break;
						}
					});
					var row = data.datas[0].attachments;
					for (var j = 0; j < data.datas[0].attachments.length; j++) {
						var txt = ['<div class="attach_list_modify mb10" fileSrc="' + row[j].fileId + '" fileName="' + row[j].fileName + '" fileSize="' + row[j].fileSize + '">',
							'<div class="attach_logo"><img src="../../../res/images/icon/logo_attach.png"></div>',
							'<div class="attach_op">',
							'<div class="attach_type">',
							'<a href="/finance/api/v1.0/downloadFile.file?fileUuid=' + row[j].fileId + '" download="' + row[j].fileName + '">',
							'附件：' + row[j].fileName + '</a></div>',
							'<div class="attach_delete">删除</div>',
							'<div class="hr"></div>',
						].join('');
						$("#attach_part3").append(txt);
					}
				} else {
					util.alert(data.message);
				};
			}
		});
	};	
	
	//修改公告信息
	var modifyAnnouceInfo = function(acceptRole, fileArray, fileNameArray, fileSizeArray) {
		if ($("#end_input_modify").val() < $("#start_input_modify").val()) {
			util.alert("开始有效日期不能小于结束有效日期");
			return;
		};
		var promptDialog = validate.validateInput($("#announce_input_modify"));
		if (promptDialog.length != 0) {
			util.alert(promptDialog);
		} else {
			var body = base.parseForm($(".announceModifyBody"));
			body.bulletinId = bulletinId;
			body.author = base.getData("sid");
			body.isEnabled = '-1';
			body.fileIds = fileArray;
			body.fileNames = fileNameArray;
			body.fileSizes = fileSizeArray;
			body.audiences = acceptRole;
			base.fnAjax({
				action: "updateBulletin.json",
				oData: body,
				fnSuccess: function(data) {
					if (data.status == 200) {
						base.alert(1, "修改成功！")
						router.route("announceManage/platform_main_announcement_manager.html");
					} else {
						util.alert(data.message);
					}
				}
			});
		}
	};
	return {
		init: init
	};
});